package com.itheimamobileguard.activitys;

import com.example.mobile.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public abstract class BaseSetUpActivity extends Activity {

	public SharedPreferences sp;
	public GestureDetector mGestureDetector;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener(){
			
			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				//e1,第一次触摸手机屏幕的事件
				//e2,手指离开屏幕一瞬间的事
				if (Math.abs(velocityY)<200) {
					Toast.makeText(getApplicationContext(), "无效动作", 0).show();
					return true;
				}
				if ((e2.getRawX()-e1.getRawX())>200) {
					//上一个
					showPre();
					overridePendingTransition(R.anim.pre_in, R.anim.pre_out);
					return true;
				}
				
				if ((e1.getRawX()-e2.getRawX())>200) {
					//下一个
					showNext();
					overridePendingTransition(R.anim.next_in, R.anim.next_out);
					return true;
				}
				
				return super.onFling(e1, e2, velocityX, velocityY);
			}
		});
	}
	
	
	//用手势识别器去识别代码
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	/**
	 * 开启和关闭activity
	 */
	public void startAndFinshActivity(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
		finish();
	}

	/**
	 * 显示下一个页面
	 */
	public abstract void showNext();

	/**
	 * 显示上一个页面
	 */
	public abstract void showPre();

	/**
	 * 下一步按钮的点击事件
	 */
	public void next(View view) {
		showNext();
		overridePendingTransition(R.anim.next_in, R.anim.next_out);
	}

	/**
	 * 上一步的点击事件
	 */
	public void pre(View view) {
		showPre();
		overridePendingTransition(R.anim.pre_in, R.anim.pre_out);
	}
}
